package com.slz3.business.module.configManagement.conVersion.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.configManagement.conVersion.domain.ConVersion;
import com.slz3.business.module.configManagement.conVersion.domain.ConVersionFactory;
import com.slz3.business.module.configManagement.conVersion.steward.ConVersionSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class ConVersionServiceImpl implements ConVersionService {
    @Override
    public ConVersion save(ConVersion conVersion) {
        return Repositorys.conVersion.save(ConVersionFactory.initBaseFields(conVersion));
    }
    @Override
    public Page<ConVersion> page(MultiValueMap<String, String> params) {
        Page<ConVersion> conVersionPage = Repositorys.conVersion.findAll(
                ConVersionSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return conVersionPage;
    }


    @Override
    public ConVersion modify(ConVersion conVersionNew) {
        return Repositorys.conVersion.saveAndFlush(ConVersionFactory.initBaseFields(conVersionNew));
    }

    @Override
    public Optional<ConVersion> single(UUID uuid) {
        return Repositorys.conVersion.findById(uuid);
    }

    @Override
    public List<ConVersion> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.conVersion.findAllById(uuids);
    }

    @Override
    public List<ConVersion> delete(List<ConVersion> conVersionList) {
        conVersionList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            ConVersionFactory.initBaseFields(item);
        });
        return Services.conVersionTransactional.batchModify(conVersionList);
    }

}
